using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;

namespace Restaurant
{
    /// <summary>
    /// Interaction logic for TableWindow.xaml
    /// </summary>
    public partial class TableWindow : Window
    {
        MainWindow mainWindow = new MainWindow();
        Table table = new Table(new List<Article>());
        StringBuilder totalOrder = new StringBuilder();
        List<Article> allArticles = new List<Article>();
        decimal orderSum = 0;

        public TableWindow()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
            customCulture.NumberFormat.NumberDecimalSeparator = ".";
            System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

            InitializeComponent();
            MakeButtons();
            HideAllStackPanels();
        }

        /// <summary>
        /// Reads the articles from a file and adds them as buttons where they belong.
        /// </summary>
        private void MakeButtons()
        {
            StringBuilder articlesBuilder = FileReader.ReadFile("../../articles.txt", "error.log");
            string[] articles = articlesBuilder.ToString().Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            var drinksItems = new List<ArticleButton>();
            var saladItems = new List<ArticleButton>();
            var soupItems = new List<ArticleButton>();
            var mealItems = new List<ArticleButton>();
            var dessertItems = new List<ArticleButton>();
            var otherItems = new List<ArticleButton>();

            foreach (var article in articles)
            {
                string[] token = article.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                token[0] = "1";
                int articleID = int.Parse(token[0].ToString().Trim());
                decimal sellingPrice = decimal.Parse(token[3].Trim());
                decimal buyingPrice = decimal.Parse(token[4].Trim());
                double quantity = double.Parse(token[5].Trim());
                switch (token[1].Trim())
                {
                    case "Drink":
                        allArticles.Add(new Drink(articleID, token[2], sellingPrice, buyingPrice, quantity));
                        drinksItems.Add(new ArticleButton(token[2], token[3]));
                        break;
                    case "Salad":
                        allArticles.Add(new Salad(articleID, token[2], sellingPrice, buyingPrice, quantity));
                        saladItems.Add(new ArticleButton(token[2], token[3]));
                        break;
                    case "Soup":
                        allArticles.Add(new Soup(articleID, token[2], sellingPrice, buyingPrice, quantity));
                        soupItems.Add(new ArticleButton(token[2], token[3]));
                        break;
                    case "Meal":
                        allArticles.Add(new Meal(articleID, token[2], sellingPrice, buyingPrice, quantity));
                        mealItems.Add(new ArticleButton(token[2], token[3]));
                        break;
                    case "Dessert":
                        allArticles.Add(new Dessert(articleID, token[2], sellingPrice, buyingPrice, quantity));
                        dessertItems.Add(new ArticleButton(token[2], token[3]));
                        break;
                    default:
                        otherItems.Add(new ArticleButton(token[2], token[3]));
                        break;
                }
            }

            this.DrinksControl.ItemsSource = drinksItems;
            this.DrinksControl.ItemsSource = drinksItems;
            this.SaladControl.ItemsSource = saladItems;
            this.SoupControl.ItemsSource = soupItems;
            this.MealControl.ItemsSource = mealItems;
            this.DessertControl.ItemsSource = dessertItems;
        }

        /// <summary>
        /// Drinks selection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DrinkButtonClick(object sender, RoutedEventArgs e)
        {
            string btnName = (sender as Button).Content.ToString();
            decimal price = decimal.Parse((sender as Button).Tag.ToString());

            var article = allArticles.FirstOrDefault(obj => obj.Name == btnName) as Drink;
            table.TableOrderList.Add(article);
            UpdateOrderTextBlock();
        }

        /// <summary>
        /// Salads selection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaladButtonClick(object sender, RoutedEventArgs e)
        {
            string btnName = (sender as Button).Content.ToString();
            decimal price = decimal.Parse((sender as Button).Tag.ToString());
            var article = allArticles.FirstOrDefault(obj => obj.Name == btnName) as Salad;
            table.TableOrderList.Add(article);
            UpdateOrderTextBlock();
        }

        /// <summary>
        /// Soups selection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SoupButtonClick(object sender, RoutedEventArgs e)
        {
            string btnName = (sender as Button).Content.ToString();
            decimal price = decimal.Parse((sender as Button).Tag.ToString());
            var article = allArticles.FirstOrDefault(obj => obj.Name == btnName) as Soup;
            table.TableOrderList.Add(article);
            UpdateOrderTextBlock();
        }

        /// <summary>
        /// Meals selection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MealButtonClick(object sender, RoutedEventArgs e)
        {
            string btnName = (sender as Button).Content.ToString();
            decimal price = decimal.Parse((sender as Button).Tag.ToString());
            var article = allArticles.FirstOrDefault(obj => obj.Name == btnName) as Meal;
            table.TableOrderList.Add(article);
            UpdateOrderTextBlock();
        }

        /// <summary>
        /// Desserts selection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DessertButtonClick(object sender, RoutedEventArgs e)
        {
            string btnName = (sender as Button).Content.ToString();
            decimal price = decimal.Parse((sender as Button).Tag.ToString().Trim());
            var article = allArticles.FirstOrDefault(obj => obj.Name == btnName) as Dessert;
            table.TableOrderList.Add(article);
            UpdateOrderTextBlock();
        }

        /// <summary>
        /// Textblock order updater
        /// </summary>
        private void UpdateOrderTextBlock()
        {
            foreach (var item in table.TableOrderList)
            {
                totalOrder.AppendLine(item.ToString());
            }
            OrderDisplayTextBlock.Text = totalOrder.ToString();
            totalOrder.Clear();
        }

        /// <summary>
        /// Order sum calclulator
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CalculateSumButton_Click(object sender, RoutedEventArgs e)
        {

            foreach (var ordererItem in table.TableOrderList)
            {
                orderSum += ordererItem.SellingPrice;
            }
            OrderTotalSumTextBlock.Text = String.Format("TOTAL: {0:C}", orderSum);
            PrintProductsList();
            mainWindow.UpdateTableColor(this.Title.ToString());
            this.Close();
        }

        /// <summary>
        /// Total bill invoice
        /// </summary>
        private void PrintProductsList()
        {
            StringBuilder billList = new StringBuilder();
            foreach (var item in table.TableOrderList)
            {
                billList.AppendLine(item.ToString());
            }
            billList.AppendLine();
            billList.AppendFormat("TOTAL: {0:C}", orderSum);
            MessageBox.Show(billList.ToString());
        }

        private void HideAllStackPanels()
        {
            DrinksStackPanel.Visibility = System.Windows.Visibility.Hidden;
            SaladsStackPanel.Visibility = System.Windows.Visibility.Hidden;
            SoupsStackPanel.Visibility = System.Windows.Visibility.Hidden;
            MealsStackPanel.Visibility = System.Windows.Visibility.Hidden;
            DessertsStackPanel.Visibility = System.Windows.Visibility.Hidden;
        }

        /// <summary>
        /// Middle column buttons event handlers
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ShowDrinksStackPanel_OnButtonClick(object sender, RoutedEventArgs e)
        {
            HideAllStackPanels();
            DrinksStackPanel.Visibility = System.Windows.Visibility.Visible;
        }

        private void ShowSaladsStackPanel_OnButtonClick(object sender, RoutedEventArgs e)
        {
            HideAllStackPanels();
            SaladsStackPanel.Visibility = System.Windows.Visibility.Visible;
        }

        private void ShowSoupsStackPanel_OnButtonClick(object sender, RoutedEventArgs e)
        {
            HideAllStackPanels();
            SoupsStackPanel.Visibility = System.Windows.Visibility.Visible;
        }

        private void ShowMealsStackPanel_OnButtonClick(object sender, RoutedEventArgs e)
        {
            HideAllStackPanels();
            MealsStackPanel.Visibility = System.Windows.Visibility.Visible;
        }

        private void ShowDessertsStackPanel_OnButtonClick(object sender, RoutedEventArgs e)
        {
            HideAllStackPanels();
            DessertsStackPanel.Visibility = System.Windows.Visibility.Visible;
        }
    }
}
